import unittest
from time import sleep
from random import randint
from selenium.webdriver.support.select import Select

from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner

class Jxc_test(unittest.TestCase):
  def setUp(self):
    # self.driver = webdriver.Chrome();
    self.driver = webdriver.Firefox()
    self.driver.get('http://localhost/jxc/index.asp')
    a = self.driver.find_elements_by_tag_name('input')
    a[0].send_keys('admin')
    a[1].send_keys('admin')
    a[2].click()

  def tearDown(self):
    self.driver.quit()

  def test_login(self):

    self.assertIn('后台管理系统', self.driver.page_source)

  def test_delete_unit(self):
    self.driver.switch_to.frame('left')
    sleep(1)
    # 点击系统设置
    self.driver.find_element_by_css_selector('.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)').click()
    sleep(1)
    # 点击计量单位管理
    self.driver.find_element_by_css_selector('#g_6 > table > tbody > tr:nth-child(7) > td > table > tbody > tr > td:nth-child(2)').click()
    sleep(1)
    self.driver.switch_to.default_content()
    self.driver.switch_to.frame('right')
    self.driver.find_element_by_name('chkall').click()
    sleep(1)
    self.driver.find_elements_by_class_name('button')[1].click()
    sleep(1)
    self.driver.switch_to.alert.accept()
    sleep(1)
    # 获取计量单位的tbody
    tbody = self.driver.find_element_by_css_selector('body > table > tbody > tr:nth-child(2) > td:nth-child(2) > form > table > tbody')
    # 获取tbody里面的tr
    tr = tbody.find_elements_by_tag_name('tr')
    self.assertEqual(3, len(tr))

  def test_add_unit(self):
    self.driver.switch_to.frame('left')
    sleep(1)
    self.driver.find_element_by_css_selector('.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)').click()
    sleep(1)
    self.driver.find_element_by_css_selector('#g_6 > table > tbody > tr:nth-child(7) > td > table > tbody > tr > td:nth-child(2)').click()
    sleep(1)
    self.driver.switch_to.default_content()
    self.driver.switch_to.frame('right')
    self.driver.find_elements_by_class_name('button')[0].click()
    sleep(1)
    dw = 'T'
    self.driver.find_element_by_name('danwei').send_keys(dw)
    self.driver.find_element_by_name('submit').click()
    sleep(1)
    self.driver.switch_to.alert.accept()
    sleep(1)
    # 获取计量单位的tbody
    tbody = self.driver.find_element_by_css_selector('body > table > tbody > tr:nth-child(2) > td:nth-child(2) > form > table > tbody')
    # 获取tbody里面的tr
    tr = tbody.find_elements_by_tag_name('tr')
    text = tr[len(tr) - 3].find_elements_by_tag_name('td')[0].text
    self.assertEqual(dw, text)

  def test_add_bigclass(self):
    sleep(1)
    self.driver.switch_to.frame('left')
    # 点击系统设置
    self.driver.find_element_by_css_selector('.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)').click()
    sleep(1)
    # 点击大类管理
    self.driver.find_element_by_css_selector('#g_6 > table > tbody > tr:nth-child(5) > td > table > tbody > tr > td:nth-child(2)').click()
    self.driver.switch_to.default_content()
    self.driver.switch_to.frame('right')
    sleep(1)
    self.driver.find_elements_by_class_name('button')[0].click()
    sleep(1)
    text = '胖的'
    self.driver.find_element_by_name('bigclass').send_keys(text)
    self.driver.find_element_by_name('submit').click()
    sleep(1)
    self.driver.switch_to.alert.accept()
    sleep(1)
    self.driver.switch_to.default_content()
    self.driver.switch_to.frame('left')
    # 回到大类管理
    self.driver.find_element_by_css_selector('#g_6 > table > tbody > tr:nth-child(5) > td > table > tbody > tr > td:nth-child(2)').click()
    sleep(1)
    self.driver.switch_to.default_content()
    self.driver.switch_to.frame('right')
    arr = [] # 定义列表以存入大类数量
    tbody = self.driver.find_element_by_css_selector('body > table > tbody > tr:nth-child(2) > td:nth-child(2) > form > table > tbody')
    sleep(1)
    # 获取tbody里面的tr
    tr = tbody.find_elements_by_tag_name('tr')
    for i in tr:
      arr.append(i.find_element_by_tag_name('td').text)
    # 删除前后固定部分
    arr.remove(arr[0])
    arr.remove(arr[len(arr) - 1])
    # 判断加入的大类文字是否再列表里
    self.assertIn(text, arr)

  def test_add_smallclass(self):
    sleep(1)
    self.driver.switch_to.frame('left')
    # 点击系统设置
    self.driver.find_element_by_css_selector('.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)').click()
    sleep(1)
    # 点击小类管理
    self.driver.find_element_by_css_selector('#g_6 > table > tbody > tr:nth-child(6) > td > table > tbody > tr > td:nth-child(2)').click()
    self.driver.switch_to.default_content()
    self.driver.switch_to.frame('right')
    sleep(1)
    tbody = self.driver.find_element_by_css_selector('body > table:nth-child(2) > tbody > tr:nth-child(2) > td:nth-child(2) > form > table > tbody')
    trlen = len(tbody.find_elements_by_tag_name('tr')) # 将小类数量保存

    self.driver.find_elements_by_class_name('button')[0].click()
    sleep(1)
    se = Select(self.driver.find_element_by_name('bigclass'))
    se.select_by_index(randint(0, len(se.options) - 1))
    sleep(1)
    self.driver.find_element_by_name('smallclass').send_keys('可乐')
    self.driver.find_element_by_name('submit').click()
    self.driver.switch_to.alert.accept()
    sleep(1)
    self.driver.switch_to.default_content()
    self.driver.switch_to.frame('left')
    # 回到小类管理
    self.driver.find_element_by_css_selector('#g_6 > table > tbody > tr:nth-child(6) > td > table > tbody > tr > td:nth-child(2)').click()
    sleep(1)
    self.driver.switch_to.default_content()
    self.driver.switch_to.frame('right')
    tbody = self.driver.find_element_by_css_selector('body > table:nth-child(2) > tbody > tr:nth-child(2) > td:nth-child(2) > form > table > tbody')
    trlen2 = len(tbody.find_elements_by_tag_name('tr'))  # 将更改后的小类数量保存
    self.assertEqual(trlen, trlen2 - 1)

def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(Jxc_test('test_login'))
  suite.addTest(Jxc_test('test_delete_unit'))
  suite.addTest(Jxc_test('test_add_unit'))
  suite.addTest(Jxc_test('test_add_bigclass'))
  suite.addTest(Jxc_test('test_add_smallclass'))
  return suite


with open('F:/T1/test.html', 'wb') as f:
  runner = HTMLTestRunner(stream=f, title="测试报告")
  runner.run(test_suite())